package com.ruoyi.hr.service.impl.salaryadmin.salaryopencorrection;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salaryadmin.SalaryAdminMapper;
import com.ruoyi.hr.mapper.salaryadmin.salaryopencorrection.SalaryOpenCorrectionMapper;
import com.ruoyi.hr.service.salaryadmin.salaryopencorrection.SalaryOpenCorrectionService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SalaryOpenCorrectionServiceImpl implements SalaryOpenCorrectionService {
    @Autowired
    SalaryAdminMapper salaryAdminMapper;

    @Autowired
    SalaryOpenCorrectionMapper salaryOpenCorrectionMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map, List<Authority> authorityList, int isAdmin) {
        List<Map<String, Object>> mapList = salaryAdminMapper.findProObject(map.get("pgid").toString());
        StringBuffer sql = new StringBuffer();
        for (Map map1 : mapList) {
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + ",");
        }
        List<Map<String, Object>> mapList1 = salaryOpenCorrectionMapper.findData(map, sql.toString(), authorityList, isAdmin);

        for (Map map1 : mapList) {
            //循环薪资项目
            String SalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString();
            for (Map map2 : mapList1) {
                //循环人
                if (!map2.containsKey(SalaryKey)) {
                    map2.put(SalaryKey, 0);
                }
            }
        }
        return mapList1;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult cstaffAdd2(String allid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allid.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据PEID取出人名
                //String name = salaryOpenCorrectionMapper.findNameByid(id);
                sql = "declare @RetVal varchar(50) exec cSP_cstaffAdd2 " + id + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = salaryOpenCorrectionMapper.procedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                /*
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
                */
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS, "添加人员成功!");
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult deleteEmp(String allid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allid.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据PEID取出人名
                String name = salaryOpenCorrectionMapper.findNameByid(id);
                sql = "declare @RetVal varchar(50) exec cSP_cstaff_toDec " + id + ",@RetVal output select @RetVal";
                String retVal = salaryOpenCorrectionMapper.procedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS, "删除员工成功!");
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult threeProcedure(String allId, String type) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = salaryOpenCorrectionMapper.findNameByid(id);
                if (type.equals("cSP_Staff_to_Start")) {
                    sql = "declare @RetVal varchar(50) exec cSP_Staff_to_Start " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                } else {
                    sql = "declare @RetVal varchar(50) exec " + type + " " + id + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                }
                String retVal = salaryOpenCorrectionMapper.procedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {

            if (type.equals("cSP_cstaff_toCheck")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "数据确认成功!");
            } else if (type.equals("cSP_cstaff_toCancel")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "取消确认成功!");
            } else if (type.equals("cSP_Staff_to_Start")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "执行成功!");
            }
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateSalary(Map map) {
        return salaryOpenCorrectionMapper.updateSalary(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findDataHistory(Map map, List<Authority> authorityList, int isAdmin) {
        List<String> proList = salaryAdminMapper.findProByPgid(map.get("pgid").toString());
        map.put("pro", proList);
        return salaryOpenCorrectionMapper.findDataHistory(map, authorityList, isAdmin);
    }
}
